package com.csii.pos.scaleplan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.exception.ActSubmitException;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.serializer.PeSerializerUtils;
import com.csii.config.esb.EsbSocketReqUtil;
import com.csii.pos.rpc.IfbScanlePlanReq;
import com.csii.pos.rpc.upp.IfbScanlePlanClient;
import com.csii.pos.scaleplan.service.IIfbScalePlanService;
import com.csii.pos.scaleplan.vo.IfbScalePlanVO;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.controller.BaseController;

import com.csii.adhoc.util.ConvertUtils;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 投融资规模计划
 * @Author: power-stdio
 * @Date:   2021-07-21
 * @Version: V1.0
 */

@RestController
@RequestMapping("/ifbScalePlan")
@Slf4j
public class IfbScalePlanController extends BaseController<IfbScalePlan, IIfbScalePlanService>{
	@Autowired
	private IIfbScalePlanService ifbScalePlanService;
	@Autowired
	private IfbScanlePlanClient ifbScanlePlanClient;
	@Autowired
	private EsbSocketReqUtil esbSocketReqUtil;

	/**
	 * 投融资规模计划查询
	 *
	 * @param ifbScalePlanVO  参数
	 * @return 分页查询结果
	 */
	@OperationLog(value = "投融资规模计划查询")
	@PostMapping(value = "/queryScalePlanList")
	public APIResult<?> queryScalePlanList(@RequestBody IfbScalePlanVO ifbScalePlanVO) {
		IfbScalePlan ifbScalePlan  = ConvertUtils.convert(IfbScalePlan.class,ifbScalePlanVO);
		QueryWrapper<IfbScalePlan> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(ifbScalePlan.getIfbPlanDate())) {
			queryWrapper.eq("IFB_PLAN_DATE", ifbScalePlan.getIfbPlanDate());
		}
		List<IfbScalePlan> list = ifbScalePlanService.list(queryWrapper);
		return APIResult.successResult(list);
	}

	 /**
	  * 通过投融资工具查询具体项目
	  *
	  * @param vo id
	  * @return 查询结果
	  */
//	 @OperationLog(value = "通过投融资工具查询具体项目")
//	 @PostMapping(value = "/queryByToolType")
//	 public APIResult<?> queryByToolType(@RequestBody IfbScalePlanVO vo) {
//		 QueryWrapper<IfbScalePlan> queryWrapper = new QueryWrapper<>();
//		 queryWrapper.eq("IFB_TOOL_TYPE",vo.getIfbToolType());
//		 List<IfbScalePlan> list = ifbScalePlanService.list(queryWrapper);
//		 list.removeIf(x -> x.getIfbToolType().equals(x.getPrdNmCns()));
//		 return APIResult.successResult(list);
//	 }

    /**
     * 编辑
     *
     * @param ifbScalePlan 参数
     * @return 编辑结果
     */
    @OperationLog(value = "投融资规模计划-编辑")
    @PostMapping(value = "/editIfbScanlePlan")
    public APIResult<?> editScalePlan(@RequestBody IfbScalePlan ifbScalePlan) {
        if (StringUtils.isEmpty(ifbScalePlan.getIfbPlanSts())) {
            throw new ParamValidityException("审批状态不能为空");
        }
        if ("1".equals(ifbScalePlan.getIfbPlanSts()) || "2".equals(ifbScalePlan.getIfbPlanSts())
                || "3".equals(ifbScalePlan.getIfbPlanSts())) throw new ActSubmitException("当前数据已开始审批或审批已结束,无法更新!");
        ifbScalePlanService.updateById(ifbScalePlan);
        return APIResult.successResult("编辑成功!");
    }

	 /**
	  *   添加
	  *
	  * @param ifbScalePlanVO  参数
	  * @return 添加结果
	  */
	 @OperationLog(value = "投融资规模计划-添加")
	 @PostMapping(value = "/addIfbScanlePlan")
	 public APIResult<?> add(@RequestBody IfbScalePlanVO ifbScalePlanVO) {
		 IfbScalePlan ifbScalePlan  = ConvertUtils.convert(IfbScalePlan.class,ifbScalePlanVO);
		 ifbScalePlan.setIfbPlanSts("0");
		 ifbScalePlanService.save(ifbScalePlan);
		 return APIResult.successResult("添加成功！");
	 }

    /**
     * 通过id删除
     *
     * @param map id
     * @return 删除结果
     */
    @OperationLog(value = "投融资规模计划-通过id删除")
    @PostMapping(value = "/deleteIfbScanlePlan")
    public APIResult<?> delete(@RequestBody Map<String, String> map) {
        IfbScalePlan ifbPlanId = ifbScalePlanService.getById(map.get("ifbPlanId"));
        boolean flag = false;
        if ("1".equals(ifbPlanId.getIfbPlanSts()) || "4".equals(ifbPlanId.getIfbPlanSts())) {
            flag = true;
        }
        if (flag) throw new ActSubmitException("选中的数据包含审批中或者已驳回状态,无法删除!");
        ifbScalePlanService.removeById(map.get("ifbPlanId"));
        return APIResult.successResult("删除成功!");
    }

	/**
	 *  批量删除
	 *
	 * @param ids id集合
	 * @return 删除结果
	 */
	@OperationLog(value = "投融资规模计划-批量删除")
	@PostMapping(value = "/deleteBatch")
	public APIResult<?> deleteBatch(@RequestBody List<IfbScalePlanVO> ids) {
		//this.ifbScalePlanService.removeByIds(ids.stream().map(IfbScalePlanVO::getId).collect(Collectors.toList()));
		return APIResult.successResult("批量删除成功!");
	}

	/** 
	 * @description: 请求接口投融资规模计划执行情况
	 * @param:   IfbScalePlan
	 * @return:  liuzhonghua
	 * @author
	 * @date:  20210831
	 */
	@RequestMapping("queryIfbScanlePlanRunInfo")
	public APIResult<?> queryIfbScanlePlanRunInfo(@RequestBody IfbScalePlan ifb) {
		Map<String, String> param = new HashMap<>();
		param.put("ifbPlanId", ifb.getIfbPlanId());
		String res = null;
		try {
			res = esbSocketReqUtil.sendRequest(param, "tranCode");
		} catch (Exception e) {
			log.error("请求esb异常", e);
			return APIResult.failResult(e, "esb请求异常");
		}
		IfbScalePlanVO ifbScalePlan = new IfbScalePlanVO();
		try {
			Document document = DocumentHelper.parseText(res);
			Element rspBody = document.getRootElement().element("rspBody");
			ifbScalePlan.setIfbPlanId(rspBody.element("ifbPlanId").getTextTrim());
			ifbScalePlan.setIfbPlanDate(rspBody.element("ifbPlanDate").getTextTrim());
			ifbScalePlan.setLqdtSltFlag(rspBody.element("lqdtSltFlag").getTextTrim());
			ifbScalePlan.setIfbScaleDesc(rspBody.element("ifbScaleDesc").getTextTrim());
			ifbScalePlan.setUpdatePlanAmt(new BigDecimal(rspBody.element("updatePlanAmt").getTextTrim()));
			ifbScalePlan.setFinishedDate(rspBody.element("finishedDate").getTextTrim());
			ifbScalePlan.setUnit(rspBody.element("unit").getTextTrim());
			ifbScalePlan.setIfbPlanSts(rspBody.element("ifbPlanSts").getTextTrim());
			ifbScalePlan.setExecPlanAmt(rspBody.element("execPlanAmt").getTextTrim());
			ifbScalePlan.setUnexecPlanAmt(new BigDecimal(rspBody.element("unexecPlanAmt").getTextTrim()));
		} catch (DocumentException e) {
			log.error("xml解析异常", e);
			return APIResult.failResult(e, "xml解析异常");
		}
		return APIResult.successResult(ifbScalePlan);
	}
	@RequestMapping("queryIfbPlanId")
	public APIResult<?> queryIfbPlanId(){
		List<String> list = ifbScalePlanService.queryIfbPlanId();
		return APIResult.successResult(list);
	}

//    /**
//    * 导出excel
//    *
//    * @param ifbScalePlanVO 参数
//    */
//    @RequestMapping(value = "/exportXls")
//    public void exportXls(HttpServletResponse response, IfbScalePlanVO ifbScalePlanVO) {
//        IfbScalePlan ifbScalePlan  = ConvertUtils.convert(IfbScalePlan.class,ifbScalePlanVO);
//         super.exportXls(response,ifbScalePlan, IfbScalePlan.class, "投融资规模计划");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
//        return APIResult.successResult(super.importExcel(file, IfbScalePlan.class));
//    }
}

